package com.malljelly.study.exception;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice //作为一个控制层的切面处理
public class GlobalExceptionHandler {
	public static final String DEFAULT_ERROR_VIEW = "error"; // 定义错误显示页，error.html
	@ExceptionHandler(Exception.class) // 所有的异常都是Exception子类
	@ResponseBody
	public com.alibaba.fastjson.JSONObject defaultErrorHandler(HttpServletRequest request,Exception e) {

		JSONObject result = new JSONObject();
		result.put("code", 500);; 	// 标记一个错误信息类型
		result.put("msg", e.getMessage());
		result.put("url", request.getRequestURL());
		return result ;
	}
/*	public ModelAndView defaultErrorHandler(HttpServletRequest request,
			Exception e) { // 出现异常之后会跳转到此方法
		ModelAndView mav = new ModelAndView(DEFAULT_ERROR_VIEW); // 设置跳转路径
		mav.addObject("exception", e); // 将异常对象传递过去
		mav.addObject("url", request.getRequestURL()); // 获得请求的路径
		return mav;
	}*/
}
